// pages/order/order.js
var md5 = require('../../utils/md5.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: "", //0 抢购，1 拼团
    active: "", // 选中标签对应的索引值
    actives: 0,
    tabList: [
      "全部", "待付款", "待发货", "待收货", "待评价"
    ],
    thumbUrl: getApp().globalData.thumbUrl,
    page: 1
  },
  onChange(event) {
    var that = this
    // console.log(event)
    that.setData({
      actives: event.currentTarget.dataset.actives
    })
    setTimeout(function () {
      that.updateData();
    }, 300)

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var actives = parseInt(options.active);
    // console.log(actives)
    var uid = wx.getStorageSync('uid');
    that.setData({
      actives: actives,
    })
    var param = {
      uid: uid,
      status: actives,
      page: 1
    };
    app.wxRequest('POST', "Index/myRobOrder", param, (res) => {
      var data = res.data;
      that.setData({
        list: data
      })
    }, (err) => {
      console.log(err.errMsg)
    })

  },
  updateData: function () {
    var that = this
    var actives = that.data.actives;
    var uid = wx.getStorageSync('uid')
    var param = {
      uid: uid,
      status: actives,
      page: 1
    };
    app.wxRequest('POST', "Index/myRobOrder", param, (res) => {
      var data = res.data;
      that.setData({
        list: data,
        page: 1
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  cancleOrder: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '取消订单',
      content: '确定要取消该订单吗？',
      showCancel: true, //是否显示取消按钮
      cancelText: "取消", //默认是“取消”
      cancelColor: '#9C9C9C', //取消文字的颜色
      confirmText: "确定", //默认是“确定”
      confirmColor: '#1AA034', //确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          var param = {
            oid: id
          };
          app.wxRequest('POST', "Index/cancelOrder", param, (res) => {
            var code = res.code;
            if (code == 1) {
              wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 1500
              })
              setTimeout(function () {
                that.updateData();
              }, 1500)
              // that.onLoad();
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    var that = this;
    that.updateData();
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 3000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    var actives = that.data.actives;
    var page = that.data.page;
    var newPage = parseInt(page) + 1;
    var list = that.data.list;
    var uid = wx.getStorageSync('uid')
    var param = {
      uid: uid,
      status: actives,
      page: newPage
    };
    app.wxRequest('POST', "Index/myRobOrder", param, (res) => {
      var data = res.data;
      var code = res.code;
      if (code == 1) {
        that.setData({
          list: list.concat(data),
          page: newPage
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1500
        })
      }

    }, (err) => {
      console.log(err.errMsg)
    })
  },
  //确认收货
  confirmOrder: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认收货',
      content: '确定要执行此操作吗？',
      showCancel: true, //是否显示取消按钮
      cancelText: "取消", //默认是“取消”
      cancelColor: '#9C9C9C', //取消文字的颜色
      confirmText: "确定", //默认是“确定”
      confirmColor: '#1AA034', //确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          var param = {
            oid: id
          };
          app.wxRequest('POST', "Index/confirmRobOrder", param, (res) => {
            var code = res.code;
            if (code == 1) {
              wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 1500
              })
              setTimeout(function () {
                that.updateData();
              }, 1500)
              // that.onLoad();
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
    // console.log(e)
  },
  commentOrder: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../addComment/addComment?id=' + id + "&type=1", //type=1抢购订单,type=2拼团订单
    })
  },
  goPay: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var param = {
      id: id
    };
    app.wxRequest('POST', "Index/payRobOrder", param, (res) => {
      var code = res.code;
      if (code == 1) {
        var returnData = res.data;
        var appId = returnData.appid;
        var timeStamp = (Date.parse(new Date()) / 1000).toString();
        var pkg = 'prepay_id=' + returnData.prepay_id;
        var nonceStr = returnData.nonce_str;
        var key = returnData.key;
        var paySign = md5.hexMD5('appId=' + appId + '&nonceStr=' + nonceStr + '&package=' + pkg + '&signType=MD5&timeStamp=' + timeStamp + "&key=" + key).toUpperCase();
        wx.requestPayment({
          timeStamp: timeStamp,
          nonceStr: nonceStr,
          package: pkg,
          signType: 'MD5',
          paySign: paySign,
          success: function () {
            wx.showToast({
              title: '支付成功',
              mask: true,
              icon: "none",
              duration: 1500
            })
            that.setData({
              actives: 2
            })
            setTimeout(function () {
              that.updateData();
            }, 1500)
          },
          fail: function (aaa) {
            wx.showToast({
              title: '支付失败',
              mask: true,
              icon: "none",
              duration: 1500
            })
          }
        })
      } else if (code == 2) {
        wx.showToast({
          title: res.msg,
          mask: true,
          icon: "none",
          duration: 1500
        })
        setTimeout(function () {
          that.updateData();
        }, 1500)
      } else {
        wx.showToast({
          title: res.msg,
          mask: true,
          icon: "none",
          duration: 1500
        })
      }
    })
    console.log(e)
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})